﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoctorReg
{
    public partial class FormMedicos : Form
    {
        public FormMedicos()
        {
            InitializeComponent();
        }

        private void FormMedicos_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoSizeColumnsMode =
   DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.EditMode =
            DataGridViewEditMode.EditProgrammatically;
            ConsultasDataSetTableAdapters.MédicosTableAdapter
            adapterMedicos = new
            ConsultasDataSetTableAdapters.MédicosTableAdapter();
            ConsultasDataSet.MédicosDataTable tabMedicos = new
            ConsultasDataSet.MédicosDataTable();
            adapterMedicos.Fill(tabMedicos);
            foreach (DataColumn coluna in tabMedicos.Columns)
            {
                dataGridView1.Columns.Add(coluna.ColumnName, coluna.ColumnName);
            }
            int idxLinha = 0, idxColuna = 0;
            foreach (DataRow linha in tabMedicos.Rows)
            {
                DataGridViewRow linhaDataGrid = new DataGridViewRow();
                dataGridView1.Rows.Add(linhaDataGrid);
                foreach (DataColumn coluna in tabMedicos.Columns)
                {
                    dataGridView1.Rows[idxLinha].Cells[idxColuna].Value =
                    tabMedicos.Rows[idxLinha][idxColuna];
                    idxColuna = idxColuna + 1;
                }
                idxLinha = idxLinha + 1;
                idxColuna = 0;
            }

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int contribuinte;
            string nome, distrito;
            contribuinte = (int)
            dataGridView1.Rows[e.RowIndex].Cells[0].Value;
            nome = (string)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
            distrito = (string)
            dataGridView1.Rows[e.RowIndex].Cells[2].Value;
            FormMedicos2 form = new FormMedicos2();
            form.Alterar(contribuinte, nome, distrito);
            form.ShowDialog();

        }

        private void eliminarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int contribuinte;
   string nome, distrito;
      //Obrigar o utilizador a selecionar  um registo por completo
   if (dataGridView1.SelectedRows.Count != 1)
   {
       MessageBox.Show("Deverá selecionar  um registo completo!");
   }
   else
   {
       contribuinte = (int)
  dataGridView1.SelectedRows[0].Cells[0].Value;
       nome =
  dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
       distrito =
  dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
       ConsultasDataSetTableAdapters.MédicosTableAdapter
       adapterMedicos = new
       ConsultasDataSetTableAdapters.MédicosTableAdapter();
       ConsultasDataSet.MédicosDataTable tabMedicos = new
       ConsultasDataSet.MédicosDataTable();
       adapterMedicos.Fill(tabMedicos);
       try
       {
           adapterMedicos.Delete(contribuinte, nome, distrito);
           MessageBox.Show("Registo eliminado com sucesso!");
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
   }
        }
    }
}
